package system;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cards.Card;
import cards.CardType;

public class CentralSystem {
	
	//Working with buying cards
	
	public static class Response {
		public int restMoney;
		public Card card;
	}
	
	public CentralSystem.Response buyCard(CardType type, int money) {
		Response response = new Response();
		if (money>=type.getCost()) {
			response.card = Card.getCard(type);
			response.restMoney = money-type.getCost();
		} else response.restMoney = money;
		return response;
	}
	
	// Working with statistic
	
	private Map<String, Stat> stat=new HashMap<String, Stat>();
	
	class Stat {
		int fail=0;
		int success=0;
		public Stat(boolean init) {
			if (init) success=1;
			else fail=1;
		}
		public String toString() {
			return "fail: "+fail+" success: "+success;
		}
	}
	
	public void sendStat(String type, boolean status) {
		if (stat.containsKey(type)) {
			Stat currStat = stat.get(type);
			if (status==true) currStat.success++;
			else currStat.fail++;
		} else {
			Stat newStat = new Stat(status);
			stat.put(type, newStat);
		}
	}
	
	public void getFullStatistic() {
		Iterator<String> iter = stat.keySet().iterator();
		while (iter.hasNext()) {
			String name = iter.next();
			System.out.println(name+": "+stat.get(name));
		}
	}
	
	public void getShortStatistic() {
		Collection<Stat> stCol = stat.values();
		Iterator<Stat> iter = stCol.iterator();
		int fail=0;
		int success=0;
		while (iter.hasNext()) {
			Stat currStat = iter.next();
			fail+=currStat.fail;
			success+=currStat.success;
		}
		System.out.println("fail: "+fail+" success: "+success);
	}
 }
